package org.jsrml.sv.mob.controller.purchaser;

import java.util.List;

import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.purchaser.command.RemoveCompanyCo;
import org.jsrml.core.purchaser.command.SaveCompanyCo;
import org.jsrml.core.purchaser.entity.Company;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.CompanyQO;
import org.jsrml.core.purchaser.service.CompanyService;
import org.jsrml.sv.mob.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/company")
public class CompanyController extends BaseController {

	@Autowired
	private CompanyService companyService;
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO create(@RequestBody SaveCompanyCo command) {
		Company company = null;
		try {
			company = companyService.createCompany(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(company);
	}

	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO modify(@RequestBody SaveCompanyCo command) {
		Company company = null;
		try {
			company = companyService.modifyCompany(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(company);
	}

	@RequestMapping(value = "/pl/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO plRemove(@RequestBody RemoveCompanyCo command) {
		try {
			companyService.plRemoveCompany(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO remove(@RequestBody RemoveCompanyCo command) {
		try {
			companyService.removeCompany(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(@RequestBody CompanyQO qo) {
		qo.setRemove(false);
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = companyService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				Company company = companyService.queryUnique(qo);
				return createSuccessResult(company);
			case 2:
				List<Company> list = companyService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = companyService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}

}
